#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include <PowerupsX>

#define PREFIX "{olive}PowerupsX {default}>{green} "

new AbilityAmmo;

new g_iBeamSprite;
new g_iHaloSprite;


public Plugin:myinfo= 
{
	name="PowerupsXL Ammo",
	author="Skydive",
	description="PowerupsX Ammo Ability",
	version="1.0",
	url=""
};

public OnMapStart()
{
	g_iBeamSprite = PrecacheModel("materials/sprites/laser.vmt");
	g_iHaloSprite = PrecacheModel("materials/sprites/halo01.vmt");
}

public OnPluginStart()
{
	NativeIncStart();
	
	AbilityAmmo = PX_RegisterAbility("AbilityAmmo");
	PX_SetAbilityName(AbilityAmmo,"Infinite Ammo");
	PX_SetAbilityDescription(AbilityAmmo,"Level 3: Upgrade. Max 6: Hidden.");
	PX_SetAbilityUsage(AbilityAmmo,"Never Run Out Of Magazines!");
	PX_AbilityUseColor(AbilityAmmo,true);
	PX_SetAbilityColor(AbilityAmmo,0,100,200,255);
	PX_SetAbilityMaxLevel(AbilityAmmo,6);
	
	HookEvent("weapon_fire",Event_WeaponFire);
}

public Event_WeaponFire(Handle:event, const String:name[],bool:dontBroadcast)
{
	new userid = GetClientOfUserId(GetEventInt(event, "userid"));
	new Level = PX_GetLevel(userid, PX_GetAbility(userid));
	if( Level >= 3 )
	{
		if(PX_GetAbility(userid) == AbilityAmmo)
		{
			new Slot1 = GetPlayerWeaponSlot(userid, CS_SLOT_PRIMARY);
			if(IsValidEntity(Slot1))
			{
				if(GetEntProp(Slot1, Prop_Data, "m_iState") == 2)
				{
					SetEntProp(Slot1, Prop_Data, "m_iClip1", GetEntProp(Slot1, Prop_Data, "m_iClip1")+1);
					if( Level < 6 )
					{
						new Float:clientOrigin[3];
						GetClientAbsOrigin(userid,clientOrigin);
						TE_SetupBeamRingPoint(clientOrigin, 10.0, 500.0, g_iBeamSprite, g_iHaloSprite, 0, 10, 0.6, 7.0, 0.5, { 150,150,255,200 }, 10, 0);
						TE_SendToAll();
					}
					return;
				}
			}
			new Slot2 = GetPlayerWeaponSlot(userid, CS_SLOT_SECONDARY);
			if(IsValidEntity(Slot2))
			{
				if(GetEntProp(Slot2, Prop_Data, "m_iState") == 2)
				{
					SetEntProp(Slot2, Prop_Data, "m_iClip1", GetEntProp(Slot2, Prop_Data, "m_iClip1")+1);
					if( Level < 6 )
					{
						new Float:clientOrigin[3];
						GetClientAbsOrigin(userid,clientOrigin);
						TE_SetupBeamRingPoint(clientOrigin, 10.0, 500.0, g_iBeamSprite, g_iHaloSprite, 0, 10, 0.6, 7.0, 0.5, { 150,150,255,200 }, 10, 0);
						TE_SendToAll();
					}
					return;
				}
			}
		}
	}
	else if( Level < 3 )
	{
		if(PX_GetAbility(userid) == AbilityAmmo)
		{
			new ActiveWeapon = GetEntPropEnt(userid, Prop_Data, "m_hActiveWeapon");
			if(IsValidEntity(ActiveWeapon))
			{
				SetEntProp(userid, Prop_Data, "m_iAmmo", 100, 4, GetEntProp(ActiveWeapon, Prop_Data, "m_iPrimaryAmmoType"));
				new Float:clientOrigin[3];
				GetClientAbsOrigin(userid,clientOrigin);
				TE_SetupBeamRingPoint(clientOrigin, 10.0, 500.0, g_iBeamSprite, g_iHaloSprite, 0, 10, 0.6, 7.0, 0.5, { 150,150,255,200 }, 10, 0);
				TE_SendToAll();
			}
		}
	}
}